package com.xbongbong.crm.perform.query.render.insert;

import com.xbongbong.crm.abstracts.AbstractCrmNewPageRenderService;
import com.xbongbong.crm.handle.explain.impl.ContractExplainHandleImpl;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.field.RelyPoJo;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.model.FormTemplateModel;
import com.xbongbong.saas.model.LinkProductRelyModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/18 19:26
 */
@Service("contractNewPageRenderServiceImpl")
public class ContractNewPageRenderServiceImpl extends AbstractCrmNewPageRenderService {

    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private LinkProductRelyModel linkProductRelyModel;
    @Resource
    private FormTemplateModel formTemplateModel;
    @Resource
    private ContractExplainHandleImpl contractExplainHandle;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }


    @Override
    public void businessHandleExplain(FormDataAddGetDTO formDataAddGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form) throws XbbException {
        String corpid = formDataAddGetDTO.getCorpid();
        for (FieldAttrEntity fieldAttrEntity : list) {
            if (Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.PRODUCT.getAttr()) && FieldTypeEnum.SELECT_PRODUCT.getType().equals(fieldAttrEntity.getFieldType())) {
                // 合同产品关联字段
                List<? extends FieldAttrEntity> fieldAttrEntities = fieldAttrEntity.getSubForm().getItems();
                for (int i = 0; i < fieldAttrEntities.size(); i++) {
                    FieldAttrEntity contractProductAttrEntity = fieldAttrEntities.get(i);
                    if (!SelectProductEnum.PRODUCT.getAttr().equals(contractProductAttrEntity.getAttr())) {
                        continue;
                    }
                    LinkInfoPojo linkInfo = contractProductAttrEntity.getLinkInfo();
                    if (Objects.nonNull(linkInfo) && Objects.nonNull(linkInfo.getLinkedAttr())) {
                        List<LinkedAttrPoJo> linkedAttrList = linkInfo.getLinkedAttr();
                        Iterator<LinkedAttrPoJo> iterator = linkedAttrList.iterator();
                        // 获取产品解释
                        PaasFormExplainEntity productExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(), corpid);
                        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(productExplainEntity.getExplains());
                        while (iterator.hasNext()) {
                            LinkedAttrPoJo linkedAttrPoJo = iterator.next();
                            FieldAttrEntity attrEntity = new FieldAttrEntity();
                            if (SelectProductEnum.STOCK.getAttr().equals(linkedAttrPoJo.getAttr())) {
                                attrEntity = explainMap.get(ProductEnum.STOCK.getAttr());
                            } else if (SelectProductEnum.PRICE.getAttr().equals(linkedAttrPoJo.getAttr())) {
                                attrEntity = explainMap.get(ProductEnum.PRICE.getAttr());
                            } else if (SelectProductEnum.UNIT.getAttr().equals(linkedAttrPoJo.getAttr())) {
                                attrEntity = explainMap.get(ProductEnum.UNIT.getAttr());
                            }
                            if (BasicConstant.ZERO.equals(attrEntity.getIsOpen())) {
                                iterator.remove();
                                if (Objects.nonNull(fieldAttrEntity.getSubForm().getItems())) {
                                    FieldAttrEntity jsonObject = fieldAttrEntity.getSubForm().getItems().get(i);
                                    if (Objects.nonNull(jsonObject) && Objects.nonNull(jsonObject.getLinkInfo())) {
                                        jsonObject.getLinkInfo().setLinkedAttr(linkedAttrList);
                                    }
                                }
                            }
                        }
                    }
                }
                DefaultAttrPoJo defaultAttr = fieldAttrEntity.getDefaultAttr();
                if (Objects.nonNull(defaultAttr)) {
                    String defaultType = defaultAttr.getDefaultType();
                    if (DefaultTypeEnum.PRODUCT_DATA_RELY.getAlias().equals(defaultType)) {
                        List<RelyPoJo> relyPoJoList = linkProductRelyModel.selectProductRely(formDataAddGetDTO.getCorpid(), formDataAddGetDTO.getFormId());
                        fieldAttrEntity.setRelyList(relyPoJoList);
                    }
                }
            }
        }
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
        BeanUtil.copyProperties(formDataAddGetDTO, formatExplainDTO);
        formatExplainDTO.setExplainList(list);
        if (Objects.nonNull(formDataAddGetDTO.getSaasSpecialParamPojo())) {
            formatExplainDTO.setLinkBusinessType(formDataAddGetDTO.getSaasSpecialParamPojo().getLinkBusinessType());
        }
        list = contractExplainHandle.newHandleExplain(formatExplainDTO);
        // 设置模板字段
        formTemplateModel.setTemplate(corpid, form, formDataAddGetDTO.getBusinessType(), formDataAddGetDTO.getLoginUser(), list, 1);

    }
}
